"""
局部最近邻点匹配
"""
import math_utility as mu


# 最近邻点匹配
def point_matching(can, tar, threshold=20, slot=5):
    # 计算匹配后的序列长度
    if len(can) > len(tar):
        if len(tar) > slot:
            min_dis = [10000] * slot
            min_match = [[-1, -1]] * slot
        else:
            min_dis = [10000] * len(tar)
            min_match = [[-1, -1]] * len(tar)
    else:
        if len(can) > slot:
            min_dis = [10000] * slot
            min_match = [[-1, -1]] * slot
        else:
            min_dis = [10000] * len(can)
            min_match = [[-1, -1]] * len(can)
    # 遍历找出最小列表
    for i in range(len(can)):
        for j in range(len(tar)):
            dis = mu.get_distance(can[i], tar[j])
            for k in range(len(min_dis)):
                if min_dis[k] > dis:
                    min_dis.pop()
                    min_dis.insert(k, dis)
                    min_match.pop()
                    min_match.insert(k, [i, j])
                    break
    # 删除多次匹配解和超过长度阈值的解
    exist = []
    length = len(min_dis)
    i = 0
    while i < length:
        if min_match[i][0] in exist or min_dis[i] > threshold or min_match[i][0] >= slot:
            min_match.pop(i)
            min_dis.pop(i)
            length -= 1
            continue
        exist.append(min_match[i][0])
        i += 1
    return min_match
